package stream

func Sort[T any](data []T, f Comparable[T]) {
	n := len(data)
	quickSort(data, 0, n, maxDepth(n), f)
}

// quickSort copy from sort.quickSort
// add Comparable param
func quickSort[T any](data []T, a, b, maxDepth int, f Comparable[T]) {
	for b-a > 12 {
		if maxDepth == 0 {
			heapSort(data, a, b, f)
			return
		}
		maxDepth--
		mlo, mhi := doPivot(data, a, b, f)
		if mlo-a < b-mhi {
			quickSort(data, a, mlo, maxDepth, f)
			a = mhi
		} else {
			quickSort(data, mhi, b, maxDepth, f)
			b = mlo
		}
	}
	if b-a > 1 {
		for i := a + 6; i < b; i++ {
			if f(data[i], data[i-6]) < 0 {
				swap(data, i, i-6)
			}
		}
		insertionSort(data, a, b, f)
	}
}

func heapSort[T any](data []T, a, b int, f func(T, T) int) {
	first := a
	lo := 0
	hi := b - a

	for i := (hi - 1) / 2; i >= 0; i-- {
		siftDown(data, i, hi, first, f)
	}

	for i := hi - 1; i >= 0; i-- {
		swap(data, first, first+i)
		siftDown(data, lo, i, first, f)
	}
}

func siftDown[T any](data []T, lo, hi, first int, f func(T, T) int) {
	root := lo
	for {
		child := 2*root + 1
		if child >= hi {
			break
		}
		if child+1 < hi && f(data[first+child], data[first+child+1]) < 0 {
			child++
		}
		if f(data[first+root], data[first+child]) >= 0 {
			return
		}
		swap(data, first+root, first+child)
		root = child
	}
}

func insertionSort[T any](data []T, a, b int, f func(T, T) int) {
	for i := a + 1; i < b; i++ {
		for j := i; j > a && f(data[j], data[j-1]) < 0; j-- {
			swap(data, j, j-1)
		}
	}
}

func doPivot[T any](data []T, lo, hi int, f func(T, T) int) (midlo, midhi int) {
	m := int(uint(lo+hi) >> 1)
	if hi-lo > 40 {
		s := (hi - lo) / 8
		medianOfThree(data, lo, lo+s, lo+2*s, f)
		medianOfThree(data, m, m-s, m+s, f)
		medianOfThree(data, hi-1, hi-1-s, hi-1-2*s, f)
	}
	medianOfThree(data, lo, m, hi-1, f)

	pivot := lo
	a, c := lo+1, hi-1

	for ; a < c && f(data[a], data[pivot]) < 0; a++ {
	}
	b := a
	for {
		for ; b < c && f(data[pivot], data[b]) >= 0; b++ {
		}
		for ; b < c && f(data[pivot], data[c-1]) < 0; c-- {
		}
		if b >= c {
			break
		}
		swap(data, b, c-1)
		b++
		c--
	}
	protect := hi-c < 5
	if !protect && hi-c < (hi-lo)/4 {
		dups := 0
		if f(data[pivot], data[hi-1]) >= 0 {
			swap(data, c, hi-1)
			c++
			dups++
		}
		if f(data[b-1], data[pivot]) >= 0 {
			b--
			dups++
		}
		if f(data[m], data[pivot]) >= 0 {
			swap(data, m, b-1)
			b--
			dups++
		}
		protect = dups > 1
	}
	if protect {
		for {
			for ; a < b && f(data[b-1], data[pivot]) >= 0; b-- {
			}
			for ; a < b && f(data[a], data[pivot]) < 0; a++ {
			}
			if a >= b {
				break
			}
			swap(data, a, b-1)
			a++
			b--
		}
	}
	swap(data, pivot, b-1)
	return b - 1, c
}

func medianOfThree[T any](data []T, m1, m0, m2 int, f func(T, T) int) {
	if f(data[m1], data[m0]) < 0 {
		swap(data, m1, m0)
	}
	if f(data[m2], data[m1]) < 0 {
		swap(data, m2, m1)
		if f(data[m1], data[m0]) < 0 {
			swap(data, m1, m0)
		}
	}
}

func maxDepth(n int) int {
	var depth int
	for i := n; i > 0; i >>= 1 {
		depth++
	}
	return depth * 2
}

func swap[T any](data []T, i, j int) {
	data[i], data[j] = data[j], data[i]
}
